console.log('start');

console.log('---------------env set from Dockerfile---------------');
console.log('NODE_ENV=%s', process.env.NODE_ENV);

console.log('---------------env set from docker-compose.yml---------------');
console.log('USER=%s', process.env.USER);
console.log('foo=%s', process.env.foo);

// hosts
var fs = require('fs');
var hosts = fs.readFileSync('/etc/hosts', 'utf8');
console.log('---------------/etc/hosts-------------');
console.log(hosts);

// list all env
console.log('---------------process.env-------------');
console.log(process.env);


// --------------try connect to redis
// redis run in another container
// we can get it ip:port from process.env or /etc/hosts

// use env
var redisConfig = process.env.REDIS_PORT;
console.log('redisConfig=%s', redisConfig);
var regex = /([^:]*):\/\/([^:]*):(\d*)/g; // "schema://host:port";
var match = regex.exec(redisConfig);
var redisHost = match[2];
var redisPort = match[3];
console.log('redisHost=%s', redisHost);
console.log('redisPort=%s', redisPort);

var redis = require('redis');

var client = redis.createClient(redisPort, redisHost);
client.set('hello', 'world', function(error, reply) {
	if (error) return console.log(error);
	console.log('redis set success');
	console.log(reply);
});

// wait 
var client2 = redis.createClient('6379', 'redis'); // use /etc/hosts
process.nextTick(function() {
	client2.get('hello', function(error, reply) {
		if (error) return console.log(error);
		console.log('redis get success');
		console.log(reply);
	});
})